package com.example.springboot.mapper;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class McDustSqlProvider {

    public String selectMcByYear(@Param("year") int year, @Param("city_name") String city_name) {
        String tableName = "mc_" + year;
        return new SQL() {{
            SELECT("*");
            FROM(tableName);
            WHERE("city_name like CONCAT('%', #{city_name}, '%')");
        }}.toString();
    }

    public String selectSnByYear(@Param("year") int year, @Param("city_name") String city_name) {
        String tableName = "sn_" + year;
        return new SQL() {{
            SELECT("*");
            FROM(tableName);
            WHERE("city_name like CONCAT('%', #{city_name}, '%')");
        }}.toString();
    }

    public String selectXcByYear(@Param("year") int year, @Param("city_name") String city_name) {
        String tableName = "xc_" + year;
        return new SQL() {{
            SELECT("*");
            FROM(tableName);
            WHERE("city_name like CONCAT('%', #{city_name}, '%')");
        }}.toString();
    }

    // 新增的通用模糊查询方法
//    public String selectAllDustWithConditions(Map<String, Object> params) {
//        List<String> tables = (List<String>) params.get("tables");
//        Map<String, Object> conditions = (Map<String, Object>) params.get("conditions");
//
//        SQL sql = new SQL();
//
//        StringBuilder unionQuery = new StringBuilder();
//        for (String table : tables) {
//            if (unionQuery.length() > 0) {
//                unionQuery.append(" UNION ALL ");
//            }
//            unionQuery.append("SELECT * FROM ").append(table);
//            if (conditions != null && !conditions.isEmpty()) {
//                unionQuery.append(" WHERE 1=1");
//                for (Map.Entry<String, Object> entry : conditions.entrySet()) {
//                    unionQuery.append(" AND ").append(entry.getKey()).append(" LIKE CONCAT('%', #{").append(entry.getKey()).append("}, '%')");
//                }
//            }
//        }
//
//        sql.SELECT("*").FROM("(" + unionQuery.toString() + ") AS combined_results");
//
//        return sql.toString();
//    }
}
